package com.lemon.cloud.extension;

import com.google.common.collect.Maps;
import com.lemon.cloud.extension.annotation.ExtPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author lemon
 * @version 1.0
 * @description: TODO
 * @date Create by lemon on 2019-09-14 21:07
 */
@Slf4j
@Component
public class ExtensionPointRepository {
    private Map<ExtensionCoordinate, ExtensionPoint> extensionRepository = Maps.newHashMap();

    /**
     * @param extensionCoordinate
     * @return com.lemon.cloud.extension.ExtensionPoint
     * @description
     * @author lemon
     * @date 2019-09-15 17:33
     */
    public ExtensionPoint getExtensionPoint(ExtensionCoordinate extensionCoordinate) {
        return this.extensionRepository.get(extensionCoordinate);
    }

    /**
     * @param extensionCoordinate
     * @param extensionPoint
     * @return void
     * @description
     * @author lemon
     * @date 2019-09-14 23:16
     */
    public void registered(ExtensionCoordinate extensionCoordinate, ExtensionPoint extensionPoint) {
        this.extensionRepository.put(extensionCoordinate, extensionPoint);
        log.info("Registration ExtensionCoordinate [{}] ExtensionPoint [{}]", extensionCoordinate.toString(), extensionPoint.getClass());
    }

    /**
     * @param extensionAnnotation
     * @param extensionPoint
     * @return void
     * @description
     * @author lemon
     * @date 2019-09-14 23:24
     */
    public void registered(ExtPoint extensionAnnotation, ExtensionPoint extensionPoint) {
        BusinessScenario businessScenario = BusinessScenario.valueOf(extensionAnnotation.bizId(), extensionAnnotation.useCase(), extensionAnnotation.scenario());
        ExtensionCoordinate extensionCoordinate = new ExtensionCoordinate(extensionPoint.getClass().getName(), businessScenario.getUniqueIdentity());
        this.registered(extensionCoordinate, extensionPoint);
    }

    /**
     * @param bizId
     * @param useCase
     * @param scenario
     * @param extensionPoint
     * @return void
     * @description
     * @author lemon
     * @date 2019-09-14 23:21
     */
    public void registered(String bizId, String useCase, String scenario, ExtensionPoint extensionPoint) {
        BusinessScenario businessScenario = BusinessScenario.valueOf(bizId, useCase, scenario);
        ExtensionCoordinate extensionCoordinate = new ExtensionCoordinate(extensionPoint.getClass().getName(), businessScenario.getUniqueIdentity());
        this.registered(extensionCoordinate, extensionPoint);
    }

    /**
     * @param bizId
     * @param extensionPoint
     * @return void
     * @description
     * @author lemon
     * @date 2019-09-14 23:20
     */
    public void registered(String bizId, ExtensionPoint extensionPoint) {
        BusinessScenario businessScenario = BusinessScenario.valueOf(bizId);
        ExtensionCoordinate extensionCoordinate = new ExtensionCoordinate(extensionPoint.getClass().getName(), businessScenario.getUniqueIdentity());
        this.registered(extensionCoordinate, extensionPoint);
    }

    /**
     * @param useCase
     * @param scenario
     * @param extensionPoint
     * @return void
     * @description
     * @author lemon
     * @date 2019-09-14 23:20
     */
    public void registered(String useCase, String scenario, ExtensionPoint extensionPoint) {
        BusinessScenario businessScenario = BusinessScenario.valueOf(useCase, scenario);
        ExtensionCoordinate extensionCoordinate = new ExtensionCoordinate(extensionPoint.getClass().getName(), businessScenario.getUniqueIdentity());
        this.registered(extensionCoordinate, extensionPoint);
    }

    /**
     * @param extensionPoint
     * @return void
     * @description
     * @author lemon
     * @date 2019-09-14 23:20
     */
    public void registered(ExtensionPoint extensionPoint) {
        BusinessScenario businessScenario = BusinessScenario.newDefault();
        ExtensionCoordinate extensionCoordinate = new ExtensionCoordinate(extensionPoint.getClass().getName(), businessScenario.getUniqueIdentity());
        this.registered(extensionCoordinate, extensionPoint);
    }
}
